package com.ysd.lis.service.eq.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.eq.EqFaultType;
import com.ysd.lis.mapper.eq.EqFaultTypeMapper;
import com.ysd.lis.request.CommonPageDto;
import com.ysd.lis.request.eq.FaultTypeDto;
import com.ysd.lis.service.eq.EqFaultTypeService;
import com.ysd.util.ToolsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 设备管理-设备类型 服务实现类
 * </p>
 *
 * @author prs
 * @since 2024-07-31
 */
@Service
public class EqFaultTypeServiceImpl extends ServiceImpl<EqFaultTypeMapper, EqFaultType> implements EqFaultTypeService {

    @Autowired
    EqFaultTypeMapper eqFaultTypeMapper;
    @Override
    public Result findFaultTypeList(CommonPageDto param) {

        LambdaQueryWrapper<EqFaultType> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(EqFaultType::getDelFlag, 0);

        if (StringUtils.isNotBlank(param.getSeachValue())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(EqFaultType::getFaultTypeName, param.getSeachValue()).or().like(EqFaultType::getFaultTypeCode, param.getSeachValue()));
        }
        List<EqFaultType> eqFaultTypes = eqFaultTypeMapper.selectList(lambdaQueryWrapper);
        return Result.succ(1, "查询成功", eqFaultTypes);
    }

    @Override
    public Result saveOrUpdateFaultType(FaultTypeDto dto) {
        List<EqFaultType> faultTypeList = dto.getFaultTypeList();
        boolean b = this.saveOrUpdateBatch(faultTypeList);
        if (b) {
            return Result.succ(1, "成功", null);
        } else {
            return Result.fail(400, "失败", "");
        }
    }

    @Override
    public Result deleteFaultType(EqFaultType eqFaultType) {
        int deleteById = eqFaultTypeMapper.deleteById(eqFaultType.getId());
        if(deleteById>0){
            return Result.succ(1, "删除成功", "");
        }else {
            return Result.fail(400, "删除失败", "");
        }
    }

    @Override
    public Result findFaultTypeTree() {
        LambdaQueryWrapper<EqFaultType> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(EqFaultType::getDelFlag, 0);

        List<EqFaultType> faultTypeList = eqFaultTypeMapper.selectList(lambdaQueryWrapper);
        Map<String, Object> resultMap = new HashMap<>();

        //只获取机构树
        List<Map> faultTypeTree = new ArrayList<>();
        for(EqFaultType o : faultTypeList){
            if(ToolsUtils.isEmpty(o.getParentId())){
                Map map = new HashMap();
                map.put("id", o.getId());
                map.put("faultTypeCode", o.getFaultTypeCode());
                map.put("faultTypeName", o.getFaultTypeName());
                map.put("parentId", o.getParentId());
                faultTypeTree.add(map);
            }
        }
        for (Map m : faultTypeTree) {
            List<Map> child = getChild(m, faultTypeList);
            m.put("children", child);
        }
        resultMap.put("faultTypeTree", faultTypeTree);
        return Result.succ(1, "查询成功", resultMap);

    }

    private List<Map> getChild(Map m, List<EqFaultType> faultTypeList) {
        List<Map> result = new ArrayList();

        for (EqFaultType to : faultTypeList) {
            if (ToolsUtils.isNotEmpty(to.getParentId()) && to.getParentId().equals(m.get("id"))) {
                Map map = new HashMap();
                map.put("id", to.getId());
                map.put("faultTypeCode", to.getFaultTypeCode());
                map.put("faultTypeName", to.getFaultTypeName());
                map.put("parentId", to.getParentId());
                map.put("parentName", m.get("faultTypeName"));
                result.add(map);
            }
        }

        for (Map ma : result) {
            ma.put("children", getChild(ma, faultTypeList));
        }
        if (result.size() == 0) {
            return null;
        }
        return result;
    }

}
